package com.ssm.comm.eureka;

import com.netflix.client.ClientFactory;
import com.netflix.loadbalancer.DynamicServerListLoadBalancer;
import com.netflix.loadbalancer.RoundRobinRule;
import com.netflix.loadbalancer.Server;

import java.util.List;

public interface ServiceAddressSelector {

    RoundRobinRule CHOOSE_RULE = new RoundRobinRule();


   default Server selectServer(String clientName ){
       // ClientFactory.getNamedLoadBalancer会缓存结果, 所以不用担心它每次都会向eureka发起查询
       return  CHOOSE_RULE.choose(((DynamicServerListLoadBalancer) ClientFactory
               .getNamedLoadBalancer(clientName)), null);
   }


    default List<Server> selectServers(String clientName ){
        return ((DynamicServerListLoadBalancer) ClientFactory
                .getNamedLoadBalancer(clientName)).getServerList(true);
    }
}
